import ddddocr
import cv2


def parse_code(path):
    ocr = ddddocr.DdddOcr(beta=True, use_gpu=True, show_ad=False)
    image = open(path, "rb").read()
    result = ocr.classification(image)
    return result


def parse_base64(path_str):
    ocr = ddddocr.DdddOcr(beta=True, use_gpu=True, show_ad=False)
    result = ocr.classification(path_str)
    return result


def parse_position(path):
    det = ddddocr.DdddOcr(ocr=False, beta=True, det=True, show_ad=False)
    with open(path, 'rb') as f:
        image = f.read()
    bboxes = det.detection(image)

    im = cv2.imread(path)

    for bbox in bboxes:
        x1, y1, x2, y2 = bbox
        im = cv2.rectangle(im, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)

    cv2.imwrite("result.jpg", im)

    return bboxes


def parse_slide(front, background):
    det = ddddocr.DdddOcr(det=False, ocr=False)

    with open(front, 'rb') as f:
        target_bytes = f.read()

    with open(background, 'rb') as f:
        background_bytes = f.read()

    res = det.slide_match(target_bytes, background_bytes)

    return res


# if __name__ == '__main__':
#     parse_base64(path_str)
#     print(parse_code("captcha.jpg"))
# print(parse_position("456.png"))
# print(parse_slide("0.png", "1.png"))
